#-*- coding = utf-8 -*-
#@Time : 2022/5/17 21:26
#@Author : BAYERN
#@File : __function__.py
#@software: PyCharm

import hashlib
import requests
import json

'''自定义函数'''

# 后端调用百度翻译
def translate(q):
    base_url = 'https://fanyi-api.baidu.com/api/trans/vip/translate'
    _q = q
    _from = 'en'
    _to = 'zh'
    _appid = '20220409001165196'
    _salt = '45775758'
    _key = 'cXRYjyc76iqXWaaS0rhO'
    encodeString = _appid+_q+_salt+_key  # 合并后准备MD5加密的字符串
    md5 = hashlib.md5()
    md5.update(encodeString.encode('utf-8'))
    _sign = md5.hexdigest()

    # 发送网络请求
    url = "{base_url}?q={_q}&from=en&to=zh&appid={_appid}&salt={_salt}&sign={_sign}".format(
        base_url=base_url,
        _q=_q,
        _appid=_appid,
        _salt=_salt,
        _sign=_sign,
    )
    response = requests.get(url=url)
    text = response.text

    return text


